<template>
  <a-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
  <a-card :bordered="false">
    <!-- 查询区域 -->
<!--    <div class="table-page-search-wrapper">-->
<!--      <a-form layout="inline">-->
<!--        <a-row :gutter="24">-->
<!--          <a-col :span="5">-->
<!--            <a-form-item label="操作类型" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <j-dict-select-tag v-model='queryParam.optionType' dictCode='option_type'-->
<!--                                 style='width: 200px'></j-dict-select-tag>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <a-col :span="5">-->
<!--            <a-form-item label="操作状态" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <j-dict-select-tag v-model='queryParam.optionStatus' dictCode='option_status'-->
<!--                                 style='width: 200px'></j-dict-select-tag>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <a-col :span="5">-->
<!--            <a-form-item label="设备序列号" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <a-input v-model="queryParam.deviceSn" placeholder="请输入设备序列号" style='width: 200px'></a-input>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <a-col :span='5'>-->
<!--            <a-form-item label="创建时间 ">-->
<!--              <a-range-picker v-model="queryParam.createTimeRange" @change="onChangeRangeTime"/>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <a-col :span='3'>-->
<!--            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">-->
<!--              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>-->
<!--              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>-->
<!--            </span>-->
<!--          </a-col>-->
<!--        </a-row>-->
<!--      </a-form>-->
<!--    </div>-->
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <span slot="action" slot-scope="text, record">
          <a @click="resendCMD(record)">重新下发</a>


        </span>
        <span slot="optionStatus" slot-scope="text,record,index">
          <span v-if="text=2" style="color: red">发送失败</span>
        </span>

      </a-table>
    </div>
  </a-card>
  </a-modal>
</template>

<script>
    import {mixinDevice} from '@/utils/mixin'
    import {JeecgListMixin} from '@/mixins/JeecgListMixin'
    import {filterObj} from '@/utils/util'
    import {getAction, postAction} from '@api/manage'
    import {initDictOptions} from '@comp/dict/JDictSelectUtil'

    export default {
        name: 'userDeviceModal',
        mixins: [JeecgListMixin, mixinDevice],

        data() {
            return {
              title:"",
                // 分页参数
                ipagination: {
                    current: 1,
                    pageSize: 10,
                    // pageSizeOptions: ['10', '20', '30'],
                    pageSizeOptions: ['10', '20', '50', '100'],
                    showTotal: (total, range) => {
                        return range[0] + '-' + range[1] + ' 共' + total + '条'
                    },
                    showQuickJumper: true,
                    showSizeChanger: true,
                    total: 0,
                    isorter: {
                        column: 'createTime',
                        order: 'desc'
                    },
                },
                /*查询区域属性*/
                queryParam: {
                    optionType: '',
                    optionStatus: '',
                    deviceSn: '',
                    opTime_begin: "",
                    opTime_end: "",
                },
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },
                confirmLoading: false,
                validatorRules: {},
                visible: false,
                description: '设备记录表管理页面',
                // 表头
                columns: [
                    {
                        title: '序号',
                        dataIndex: '',
                        key: 'rowIndex',
                        width: 60,
                        align: "center",
                        customRender: function (t, r, index) {
                            return parseInt(index) + 1;
                        }
                    },
                    {
                        title: '用户名',
                        align: "center",
                        dataIndex: 'userName'
                    },
                    {
                        title: '操作类型',
                        align: "center",
                        dataIndex: 'optionType_dictText'
                    },
                    {
                        title: '操作状态',
                        align: "center",
                        dataIndex: 'optionStatus',
                        scopedSlots: {customRender: 'optionStatus'}
                    },
                    {
                        title: '设备序列号',
                        align: "center",
                        dataIndex: 'deviceSn'
                    },
                    {
                        title: '错误日志',
                        align: "center",
                        dataIndex: 'errorInfo'
                    },
                    {
                        title: '创建时间',
                        align: 'center',
                        dataIndex: 'createTime',
                        sorter: true
                    },
                    {
                        title: '操作',
                        dataIndex: 'action',
                        align: "center",
                        fixed: "right",
                        width: 147,
                        scopedSlots: {customRender: 'action'}
                    }
                ],
                url: {
                    resendSinglelist: "/cmdLog/resendSinglelist",
                    resendcmd: "/cmdLog/resendcmd"
                },
                dictOptions: {},
                superFieldList: [],
            }
        },
        created() {
        },
        methods: {
            showIt(record){
                debugger
                this.visible=true;
                this.loadDatas(record)
            },
            handleOk(){

            },
            handleCancel(){
                this.close();
            },
            close(){
                this.$emit('close');
                this.visible = false;
            },
            resendCMD(record) {
                this.loading = true;
                postAction(this.url.resendcmd, record).then((res) => {
                    if (res.success) {
                        debugger
                        this.$message.success(res.message)
                    } else {
                        this.$message.warning(res.message)
                    }
                });
                this.loading = false;
            },
            onChangeRangeTime(date, dateString) {
                this.queryParam.opTime_begin = dateString[0];
                this.queryParam.opTime_end = dateString[1];
            },
            searchQuery() {
                let startTime = this.queryParam.opTime_begin;
                let endTime = this.queryParam.opTime_end;
                console.log("----------" + this.queryParam.deviceType);
                if (startTime != "" && endTime != "") {
                    if (startTime > endTime) {
                        this.$message.error("开始时间不能大于结束时间");
                        return
                    }
                }

                if (startTime == "" && endTime != "") {
                    this.$message.error("请选择开始时间");
                    return
                }

                if (startTime != "" && endTime == "") {
                    this.$message.error("请选择结束时间");
                    return
                }

                this.loadDatas(1);
            },
            getQueryParams() {
                var param = Object.assign({}, this.isorter, this.queryParam);
                param.field = this.getQueryField();
                param.pageNo = this.ipagination.current;
                param.pageSize = this.ipagination.pageSize;
                if (this.queryParam.opTime_begin != "" && this.queryParam.opTime_end != "") {
                    if (this.queryParam.opTime_begin != undefined) {
                        param.opTime_begin = this.queryParam.opTime_begin + " 00:00:00";
                    }
                    if (this.queryParam.opTime_end != undefined) {
                        param.opTime_end = this.queryParam.opTime_end + " 23:59:59";
                    }
                }

                return filterObj(param);
            },
            /*展示数据*/
            loadDatas(record) {
                console.log(record)
                console.log(record.id)
          let userId =record.id;
                debugger
                getAction(this.url.resendSinglelist, {pageNo:this.ipagination.current,pageSize:this.ipagination.pageSize,userId:userId}).then((res) => {
                    console.log(this.url.resendSinglelist)
                    if (res.success && res.result != null) {
                        console.log(res.result)
                        this.dataSource = res.result.records;
                        this.ipagination.total = res.result.total;
                    }
                    if (res.code === 510) {
                        this.$message.warning(res.message)
                    }
                    this.loading = false;
                })
            },
        }
    }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>